{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3a7aba34",
   "metadata": {},
   "outputs": [],
   "source": [
    "###4.4.1###\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "769e8474",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   国家     人口\n",
       "0  中国  14.22\n",
       "1  美国   3.00\n",
       "2  日本   1.29"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1 = {'国家':['中国','美国','日本'],\n",
    "     '人口':[14.22,3,1.29]}\n",
    "df1 = pd.DataFrame(d1)\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "004c9276",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3  俄罗斯   1.40"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#插入行#\n",
    "df1.loc[3] = {'国家':'俄罗斯','人口':1.4}\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bda654fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3  俄罗斯   1.40"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2=df1\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "343c3ac1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3  俄罗斯   1.40\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3  俄罗斯   1.40"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#拼接两个数据框#\n",
    "new_df = pd.concat([df1,df2])\n",
    "new_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4b4f2544",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3  俄罗斯   1.40\n",
       "4   中国  14.22\n",
       "5   美国   3.00\n",
       "6   日本   1.29\n",
       "7  俄罗斯   1.40"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "new_df = pd.concat([df1,df2],ignore_index= True)\n",
    "new_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "189cd622",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4     True\n",
      "5     True\n",
      "6     True\n",
      "7     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "#标出重复行#\n",
    "print (new_df.duplicated())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ff5a49d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3  俄罗斯   1.40"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#去除重复行#\n",
    "du_df = new_df.drop_duplicates()\n",
    "du_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "382028ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   国家     人口\n",
       "0  中国  14.22\n",
       "1  美国   3.00\n",
       "2  日本   1.29\n",
       "3  沙特   0.60"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "du_df.loc[3] = {'国家':'沙特', '人口':0.6}\n",
    "du_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "42fa65fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/2g/3qnpkzmn4rl2bh5s1c1nfv_m0000gn/T/ipykernel_48556/42111674.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  du_df.loc[4] = {'国家':'俄罗斯','人口':1.4}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3   沙特   0.60\n",
       "4  俄罗斯   1.40"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "du_df.loc[4] = {'国家':'俄罗斯','人口':1.4}\n",
    "du_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eac034f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/2g/3qnpkzmn4rl2bh5s1c1nfv_m0000gn/T/ipykernel_48556/4074396708.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  du_df['面积'] = pd.Series([960,937,37,23,980])\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "      <td>937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积\n",
       "0   中国  14.22  960\n",
       "1   美国   3.00  937\n",
       "2   日本   1.29   37\n",
       "3   沙特   0.60   23\n",
       "4  俄罗斯   1.40  980"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#插入列#\n",
    "du_df['面积'] = pd.Series([960,937,37,23,980])\n",
    "du_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f9cff7d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/2g/3qnpkzmn4rl2bh5s1c1nfv_m0000gn/T/ipykernel_48556/2446530816.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  du_df.loc[:,'人均面积'] = du_df['面积']/du_df['人口']\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>人均面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "      <td>67.510549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "      <td>937</td>\n",
       "      <td>312.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>28.682171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>38.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>700.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积        人均面积\n",
       "0   中国  14.22  960   67.510549\n",
       "1   美国   3.00  937  312.333333\n",
       "2   日本   1.29   37   28.682171\n",
       "3   沙特   0.60   23   38.333333\n",
       "4  俄罗斯   1.40  980  700.000000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "du_df.loc[:,'人均面积'] = du_df['面积']/du_df['人口']\n",
    "du_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "850d51aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>人均面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "      <td>67.510549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "      <td>937</td>\n",
       "      <td>312.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>28.682171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>38.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>700.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积        人均面积\n",
       "0   中国  14.22  960   67.510549\n",
       "1   美国   3.00  937  312.333333\n",
       "2   日本   1.29   37   28.682171\n",
       "3   沙特   0.60   23   38.333333\n",
       "4  俄罗斯   1.40  980  700.000000"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用assign创建新列\n",
    "du_df.assign(GDP = [1.0,12.5,3.3,5,4])\n",
    "du_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "524178ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>人均面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "      <td>67.510549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "      <td>937</td>\n",
       "      <td>312.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>28.682171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>38.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>700.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积        人均面积\n",
       "0   中国  14.22  960   67.510549\n",
       "1   美国   3.00  937  312.333333\n",
       "2   日本   1.29   37   28.682171\n",
       "3   沙特   0.60   23   38.333333\n",
       "4  俄罗斯   1.40  980  700.000000"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_excel = du_df.to_excel('../datasets/nation.xlsx',index = False)\n",
    "i = pd.read_excel('../datasets/nation.xlsx')\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "99d4e504",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 4.4.2####"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6fd27f34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>人均面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>28.682171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>38.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>700.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家    人口   面积        人均面积\n",
       "2   日本  1.29   37   28.682171\n",
       "3   沙特  0.60   23   38.333333\n",
       "4  俄罗斯  1.40  980  700.000000"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new = i.drop([0,1])\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a499071f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "      <td>937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积\n",
       "0   中国  14.22  960\n",
       "1   美国   3.00  937\n",
       "2   日本   1.29   37\n",
       "3   沙特   0.60   23\n",
       "4  俄罗斯   1.40  980"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new = i.drop(columns='人均面积')\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6c4deb2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 4.4.3 ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c3b5a99e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   国家     人口   面积\n",
       "0  中国  14.22  960"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new[0:1]  #取第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "03f13201",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "      <td>937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   国家    人口   面积\n",
       "1  美国  3.00  937\n",
       "2  日本  1.29   37\n",
       "3  沙特  0.60   23"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取索引大于等于1小于4的行\n",
    "i_new[1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8989f113",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     中国\n",
       "1     美国\n",
       "2     日本\n",
       "3     沙特\n",
       "4    俄罗斯\n",
       "Name: 国家, dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第一列\n",
    "i_new['国家']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "28ed2b48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['国家', '人口', '面积'], dtype='object')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new.columns[:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d20f23a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>980</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家   面积\n",
       "0   中国  960\n",
       "1   美国  937\n",
       "2   日本   37\n",
       "3   沙特   23\n",
       "4  俄罗斯  980"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new[['国家','面积']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "695903fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口\n",
       "0   中国  14.22\n",
       "1   美国   3.00\n",
       "2   日本   1.29\n",
       "3   沙特   0.60\n",
       "4  俄罗斯   1.40"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取连续多列数据\n",
    "i_new[i_new.columns[:2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c1e560fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 4.4.4 ###\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "416d53e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>5.30</td>\n",
       "      <td>937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积\n",
       "0   中国  14.22  960\n",
       "1   美国   5.30  937\n",
       "2   日本   1.29   37\n",
       "3   沙特   0.60   23\n",
       "4  俄罗斯   1.40  980"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过行列索引修改某个指定值\n",
    "i_new.loc[1, '人口'] = 5.3\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "baddc71d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>国家2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "      <td>China</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>5.30</td>\n",
       "      <td>937</td>\n",
       "      <td>美国</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>日本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>沙特</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>俄罗斯</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积    国家2\n",
       "0   中国  14.22  960  China\n",
       "1   美国   5.30  937     美国\n",
       "2   日本   1.29   37     日本\n",
       "3   沙特   0.60   23     沙特\n",
       "4  俄罗斯   1.40  980    俄罗斯"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改整列\n",
    "i_new.loc [:, '国家2'] = ['China','美国','日本','沙特','俄罗斯']\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3a9a8724",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>5.30</td>\n",
       "      <td>937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积\n",
       "0   中国  14.22  960\n",
       "1   美国   5.30  937\n",
       "2   日本   1.29   37\n",
       "3   沙特   0.60   23\n",
       "4  俄罗斯   1.40  980"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new = i_new.drop(columns='国家2')\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1113d011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>人均国土面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "      <td>67.510549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>5.30</td>\n",
       "      <td>937</td>\n",
       "      <td>176.792453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>28.682171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>38.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>700.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    国家     人口   面积      人均国土面积\n",
       "0   中国  14.22  960   67.510549\n",
       "1   美国   5.30  937  176.792453\n",
       "2   日本   1.29   37   28.682171\n",
       "3   沙特   0.60   23   38.333333\n",
       "4  俄罗斯   1.40  980  700.000000"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new.loc[: , '人均国土面积'] = i_new['面积']/ i_new ['人口']\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5782a461",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>国家</th>\n",
       "      <th>人口</th>\n",
       "      <th>面积</th>\n",
       "      <th>人均国土面积</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>中国</td>\n",
       "      <td>14.22</td>\n",
       "      <td>960</td>\n",
       "      <td>67.510549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>美国</td>\n",
       "      <td>5.30</td>\n",
       "      <td>937</td>\n",
       "      <td>176.792453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>日本</td>\n",
       "      <td>1.29</td>\n",
       "      <td>37</td>\n",
       "      <td>28.682171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沙特</td>\n",
       "      <td>0.60</td>\n",
       "      <td>23</td>\n",
       "      <td>38.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>俄罗斯</td>\n",
       "      <td>1.40</td>\n",
       "      <td>980</td>\n",
       "      <td>700.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>vietnam</td>\n",
       "      <td>1.10</td>\n",
       "      <td>23</td>\n",
       "      <td>32.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        国家     人口   面积      人均国土面积\n",
       "0       中国  14.22  960   67.510549\n",
       "1       美国   5.30  937  176.792453\n",
       "2       日本   1.29   37   28.682171\n",
       "3       沙特   0.60   23   38.333333\n",
       "4      俄罗斯   1.40  980  700.000000\n",
       "5  vietnam   1.10   23   32.000000"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_new.loc[5] = ['vietnam',1.1,23,32]\n",
    "i_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "17e8e239",
   "metadata": {},
   "outputs": [],
   "source": [
    "###4.4.5 ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "20ff17a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepalLength</th>\n",
       "      <th>sepalWidth</th>\n",
       "      <th>petalLength</th>\n",
       "      <th>petalWidth</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepalLength  sepalWidth  petalLength  petalWidth    species\n",
       "0            5.1         3.5          1.4         0.2     setosa\n",
       "1            4.9         3.0          1.4         0.2     setosa\n",
       "2            4.7         3.2          1.3         0.2     setosa\n",
       "3            4.6         3.1          1.5         0.2     setosa\n",
       "4            5.0         3.6          1.4         0.2     setosa\n",
       "..           ...         ...          ...         ...        ...\n",
       "145          6.7         3.0          5.2         2.3  virginica\n",
       "146          6.3         2.5          5.0         1.9  virginica\n",
       "147          6.5         3.0          5.2         2.0  virginica\n",
       "148          6.2         3.4          5.4         2.3  virginica\n",
       "149          5.9         3.0          5.1         1.8  virginica\n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_iris = pd.read_json('../datasets/iris/iris.json')\n",
    "d_iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "12a8468b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.843333333333335"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#平均值\n",
    "d_iris['sepalLength'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e859b25c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0    10\n",
       "5.1     9\n",
       "6.3     9\n",
       "5.7     8\n",
       "6.7     8\n",
       "5.8     7\n",
       "5.5     7\n",
       "6.4     7\n",
       "4.9     6\n",
       "5.4     6\n",
       "6.1     6\n",
       "6.0     6\n",
       "5.6     6\n",
       "4.8     5\n",
       "6.5     5\n",
       "6.2     4\n",
       "7.7     4\n",
       "6.9     4\n",
       "4.6     4\n",
       "5.2     4\n",
       "5.9     3\n",
       "4.4     3\n",
       "7.2     3\n",
       "6.8     3\n",
       "6.6     2\n",
       "4.7     2\n",
       "7.6     1\n",
       "7.4     1\n",
       "7.3     1\n",
       "7.0     1\n",
       "7.1     1\n",
       "5.3     1\n",
       "4.3     1\n",
       "4.5     1\n",
       "7.9     1\n",
       "Name: sepalLength, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##数据分布\n",
    "d_iris['sepalLength'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5a0b4a65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "setosa        50\n",
       "versicolor    50\n",
       "virginica     50\n",
       "Name: species, dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##数据分布\n",
    "d_iris['species'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "288c0698",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepalLength</th>\n",
       "      <th>sepalWidth</th>\n",
       "      <th>petalLength</th>\n",
       "      <th>petalWidth</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>sepalLength</th>\n",
       "      <td>0.685694</td>\n",
       "      <td>-0.042434</td>\n",
       "      <td>1.274315</td>\n",
       "      <td>0.516271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sepalWidth</th>\n",
       "      <td>-0.042434</td>\n",
       "      <td>0.189979</td>\n",
       "      <td>-0.329656</td>\n",
       "      <td>-0.121639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>petalLength</th>\n",
       "      <td>1.274315</td>\n",
       "      <td>-0.329656</td>\n",
       "      <td>3.116278</td>\n",
       "      <td>1.295609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>petalWidth</th>\n",
       "      <td>0.516271</td>\n",
       "      <td>-0.121639</td>\n",
       "      <td>1.295609</td>\n",
       "      <td>0.581006</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             sepalLength  sepalWidth  petalLength  petalWidth\n",
       "sepalLength     0.685694   -0.042434     1.274315    0.516271\n",
       "sepalWidth     -0.042434    0.189979    -0.329656   -0.121639\n",
       "petalLength     1.274315   -0.329656     3.116278    1.295609\n",
       "petalWidth      0.516271   -0.121639     1.295609    0.581006"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 相关性--协方差\n",
    "d_iris.cov()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3f0e4009",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepalLength</th>\n",
       "      <th>sepalWidth</th>\n",
       "      <th>petalLength</th>\n",
       "      <th>petalWidth</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>sepalLength</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.117570</td>\n",
       "      <td>0.871754</td>\n",
       "      <td>0.817941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sepalWidth</th>\n",
       "      <td>-0.117570</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.428440</td>\n",
       "      <td>-0.366126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>petalLength</th>\n",
       "      <td>0.871754</td>\n",
       "      <td>-0.428440</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.962865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>petalWidth</th>\n",
       "      <td>0.817941</td>\n",
       "      <td>-0.366126</td>\n",
       "      <td>0.962865</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             sepalLength  sepalWidth  petalLength  petalWidth\n",
       "sepalLength     1.000000   -0.117570     0.871754    0.817941\n",
       "sepalWidth     -0.117570    1.000000    -0.428440   -0.366126\n",
       "petalLength     0.871754   -0.428440     1.000000    0.962865\n",
       "petalWidth      0.817941   -0.366126     0.962865    1.000000"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 相关性--相关系数\n",
    "d_iris.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "1c3c68d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepalLength</th>\n",
       "      <th>sepalWidth</th>\n",
       "      <th>petalLength</th>\n",
       "      <th>petalWidth</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepalLength  sepalWidth  petalLength  petalWidth    species\n",
       "0            5.1         3.5          1.4         0.2     setosa\n",
       "1            4.9         3.0          1.4         0.2     setosa\n",
       "2            4.7         3.2          1.3         0.2     setosa\n",
       "3            4.6         3.1          1.5         0.2     setosa\n",
       "4            5.0         3.6          1.4         0.2     setosa\n",
       "..           ...         ...          ...         ...        ...\n",
       "145          6.7         3.0          5.2         2.3  virginica\n",
       "146          6.3         2.5          5.0         1.9  virginica\n",
       "147          6.5         3.0          5.2         2.0  virginica\n",
       "148          6.2         3.4          5.4         2.3  virginica\n",
       "149          5.9         3.0          5.1         1.8  virginica\n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_iris\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f1b30f0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepalLength</th>\n",
       "      <th>sepalWidth</th>\n",
       "      <th>petalLength</th>\n",
       "      <th>petalWidth</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>6.3</td>\n",
       "      <td>3.3</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>109</th>\n",
       "      <td>7.2</td>\n",
       "      <td>3.6</td>\n",
       "      <td>6.1</td>\n",
       "      <td>2.5</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.3</td>\n",
       "      <td>5.7</td>\n",
       "      <td>2.5</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>114</th>\n",
       "      <td>5.8</td>\n",
       "      <td>2.8</td>\n",
       "      <td>5.1</td>\n",
       "      <td>2.4</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.1</td>\n",
       "      <td>5.6</td>\n",
       "      <td>2.4</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>4.8</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.1</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>4.3</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.1</td>\n",
       "      <td>0.1</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.1</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>5.2</td>\n",
       "      <td>4.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.1</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.1</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepalLength  sepalWidth  petalLength  petalWidth    species\n",
       "100          6.3         3.3          6.0         2.5  virginica\n",
       "109          7.2         3.6          6.1         2.5  virginica\n",
       "144          6.7         3.3          5.7         2.5  virginica\n",
       "114          5.8         2.8          5.1         2.4  virginica\n",
       "140          6.7         3.1          5.6         2.4  virginica\n",
       "..           ...         ...          ...         ...        ...\n",
       "12           4.8         3.0          1.4         0.1     setosa\n",
       "13           4.3         3.0          1.1         0.1     setosa\n",
       "37           4.9         3.6          1.4         0.1     setosa\n",
       "32           5.2         4.1          1.5         0.1     setosa\n",
       "9            4.9         3.1          1.5         0.1     setosa\n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 4.4.6###\n",
    "d_iris.sort_values(['petalWidth'], ascending= False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "3c0e3c26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepalLength</th>\n",
       "      <th>sepalWidth</th>\n",
       "      <th>petalLength</th>\n",
       "      <th>petalWidth</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>setosa</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepalLength  sepalWidth  petalLength  petalWidth    species\n",
       "149          5.9         3.0          5.1         1.8  virginica\n",
       "148          6.2         3.4          5.4         2.3  virginica\n",
       "147          6.5         3.0          5.2         2.0  virginica\n",
       "146          6.3         2.5          5.0         1.9  virginica\n",
       "145          6.7         3.0          5.2         2.3  virginica\n",
       "..           ...         ...          ...         ...        ...\n",
       "4            5.0         3.6          1.4         0.2     setosa\n",
       "3            4.6         3.1          1.5         0.2     setosa\n",
       "2            4.7         3.2          1.3         0.2     setosa\n",
       "1            4.9         3.0          1.4         0.2     setosa\n",
       "0            5.1         3.5          1.4         0.2     setosa\n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_iris.sort_index(ascending= False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "81943f3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Market Cap</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Feb 01, 2021</th>\n",
       "      <td>33114.58</td>\n",
       "      <td>34638.21</td>\n",
       "      <td>32384.23</td>\n",
       "      <td>33537.18</td>\n",
       "      <td>61,400,400,660</td>\n",
       "      <td>624,349,044,409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jan 31, 2021</th>\n",
       "      <td>34270.88</td>\n",
       "      <td>34288.33</td>\n",
       "      <td>32270.18</td>\n",
       "      <td>33114.36</td>\n",
       "      <td>52,754,542,671</td>\n",
       "      <td>616,452,744,533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jan 30, 2021</th>\n",
       "      <td>34295.94</td>\n",
       "      <td>34834.71</td>\n",
       "      <td>32940.19</td>\n",
       "      <td>34269.52</td>\n",
       "      <td>65,141,828,798</td>\n",
       "      <td>637,924,573,284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jan 29, 2021</th>\n",
       "      <td>34318.67</td>\n",
       "      <td>38406.26</td>\n",
       "      <td>32064.81</td>\n",
       "      <td>34316.39</td>\n",
       "      <td>117,894,572,511</td>\n",
       "      <td>638,768,671,362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jan 28, 2021</th>\n",
       "      <td>30441.04</td>\n",
       "      <td>31891.30</td>\n",
       "      <td>30023.21</td>\n",
       "      <td>31649.61</td>\n",
       "      <td>78,948,162,368</td>\n",
       "      <td>589,083,045,078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>May 03, 2013</th>\n",
       "      <td>106.25</td>\n",
       "      <td>108.13</td>\n",
       "      <td>79.10</td>\n",
       "      <td>97.75</td>\n",
       "      <td>0</td>\n",
       "      <td>1,085,995,169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>May 02, 2013</th>\n",
       "      <td>116.38</td>\n",
       "      <td>125.60</td>\n",
       "      <td>92.28</td>\n",
       "      <td>105.21</td>\n",
       "      <td>0</td>\n",
       "      <td>1,168,517,495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>May 01, 2013</th>\n",
       "      <td>139.00</td>\n",
       "      <td>139.89</td>\n",
       "      <td>107.72</td>\n",
       "      <td>116.99</td>\n",
       "      <td>0</td>\n",
       "      <td>1,298,954,594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Apr 30, 2013</th>\n",
       "      <td>144.00</td>\n",
       "      <td>146.93</td>\n",
       "      <td>134.05</td>\n",
       "      <td>139.00</td>\n",
       "      <td>0</td>\n",
       "      <td>1,542,813,125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Apr 29, 2013</th>\n",
       "      <td>134.44</td>\n",
       "      <td>147.49</td>\n",
       "      <td>134.00</td>\n",
       "      <td>144.54</td>\n",
       "      <td>0</td>\n",
       "      <td>1,603,768,865</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2836 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Open      High       Low     Close            Volume  \\\n",
       "Date                                                                     \n",
       "Feb 01, 2021  33114.58  34638.21  32384.23  33537.18    61,400,400,660   \n",
       "Jan 31, 2021  34270.88  34288.33  32270.18  33114.36    52,754,542,671   \n",
       "Jan 30, 2021  34295.94  34834.71  32940.19  34269.52    65,141,828,798   \n",
       "Jan 29, 2021  34318.67  38406.26  32064.81  34316.39   117,894,572,511   \n",
       "Jan 28, 2021  30441.04  31891.30  30023.21  31649.61    78,948,162,368   \n",
       "...                ...       ...       ...       ...               ...   \n",
       "May 03, 2013    106.25    108.13     79.10     97.75                 0   \n",
       "May 02, 2013    116.38    125.60     92.28    105.21                 0   \n",
       "May 01, 2013    139.00    139.89    107.72    116.99                 0   \n",
       "Apr 30, 2013    144.00    146.93    134.05    139.00                 0   \n",
       "Apr 29, 2013    134.44    147.49    134.00    144.54                 0   \n",
       "\n",
       "                    Market Cap  \n",
       "Date                            \n",
       "Feb 01, 2021   624,349,044,409  \n",
       "Jan 31, 2021   616,452,744,533  \n",
       "Jan 30, 2021   637,924,573,284  \n",
       "Jan 29, 2021   638,768,671,362  \n",
       "Jan 28, 2021   589,083,045,078  \n",
       "...                        ...  \n",
       "May 03, 2013     1,085,995,169  \n",
       "May 02, 2013     1,168,517,495  \n",
       "May 01, 2013     1,298,954,594  \n",
       "Apr 30, 2013     1,542,813,125  \n",
       "Apr 29, 2013     1,603,768,865  \n",
       "\n",
       "[2836 rows x 6 columns]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 4.4.7 ###\n",
    "\n",
    "#指定索引\n",
    "d_bitcoin = pd.read_excel('../datasets/bitcoin/bitcoin.xlsx', index_col ='Date' )\n",
    "d_bitcoin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "392608a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算股票增长率\n",
    "# p0 = d_bitcoin['Open']\n",
    "# p1 = d_bitcoin['Close']\n",
    "\n",
    "def change_rate(d_bitcoin):\n",
    "    return ((d_bitcoin['Close']- d_bitcoin['Open'])/d_bitcoin['Open']*100)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "bcad1121",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "Nov 18, 2013    41.681099\n",
       "Dec 19, 2013    33.310215\n",
       "Dec 07, 2017    25.470171\n",
       "Jul 20, 2017    24.129363\n",
       "Nov 21, 2013    21.555728\n",
       "                  ...    \n",
       "Dec 16, 2013   -19.806209\n",
       "Dec 06, 2013   -20.427291\n",
       "Jan 14, 2015   -20.452008\n",
       "Dec 18, 2013   -22.928340\n",
       "Mar 12, 2020   -37.186901\n",
       "Length: 2836, dtype: float64"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#d_bitcoin.apply(change_rate, axis=0).sort_values('Date',ascending= True)\n",
    "d_bitcoin.apply(change_rate, axis=1).sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "48b18d22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(d_bitcoin['Close'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "c69207d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "Mar 12, 2020   -0.371869\n",
       "Dec 18, 2013   -0.229283\n",
       "Jan 14, 2015   -0.204520\n",
       "Dec 06, 2013   -0.204273\n",
       "Dec 16, 2013   -0.198062\n",
       "                  ...   \n",
       "Nov 21, 2013    0.215557\n",
       "Jul 20, 2017    0.241294\n",
       "Dec 07, 2017    0.254702\n",
       "Dec 19, 2013    0.333102\n",
       "Nov 18, 2013    0.416811\n",
       "Length: 2836, dtype: float64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cal_change_rate(x):\n",
    "    '''\n",
    "    定义一个函数，计算当日股票价格的增长率。\n",
    "    '''\n",
    "    return (x['Close'] - x['Open']) / x['Open']\n",
    "#使用自定义函数，操作数据框。\n",
    "df = d_bitcoin.apply(cal_change_rate, axis=1).sort_values()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "ab540a10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Mar 12, 2020</th>\n",
       "      <td>-37.19%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 18, 2013</th>\n",
       "      <td>-22.93%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jan 14, 2015</th>\n",
       "      <td>-20.45%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 06, 2013</th>\n",
       "      <td>-20.43%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 16, 2013</th>\n",
       "      <td>-19.81%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Nov 21, 2013</th>\n",
       "      <td>21.56%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jul 20, 2017</th>\n",
       "      <td>24.13%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 07, 2017</th>\n",
       "      <td>25.47%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 19, 2013</th>\n",
       "      <td>33.31%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Nov 18, 2013</th>\n",
       "      <td>41.68%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2836 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                    0\n",
       "Date                 \n",
       "Mar 12, 2020  -37.19%\n",
       "Dec 18, 2013  -22.93%\n",
       "Jan 14, 2015  -20.45%\n",
       "Dec 06, 2013  -20.43%\n",
       "Dec 16, 2013  -19.81%\n",
       "...               ...\n",
       "Nov 21, 2013   21.56%\n",
       "Jul 20, 2017   24.13%\n",
       "Dec 07, 2017   25.47%\n",
       "Dec 19, 2013   33.31%\n",
       "Nov 18, 2013   41.68%\n",
       "\n",
       "[2836 rows x 1 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 小数改为百分数\n",
    "df = pd.DataFrame(df)\n",
    "df[0] = df[0].apply(lambda x:format(x,'.2%'))\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "20089331",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=1, step=1)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "be2114de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>比例</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Mar 12, 2020</th>\n",
       "      <td>-37.19%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 18, 2013</th>\n",
       "      <td>-22.93%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jan 14, 2015</th>\n",
       "      <td>-20.45%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 06, 2013</th>\n",
       "      <td>-20.43%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 16, 2013</th>\n",
       "      <td>-19.81%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Nov 21, 2013</th>\n",
       "      <td>21.56%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jul 20, 2017</th>\n",
       "      <td>24.13%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 07, 2017</th>\n",
       "      <td>25.47%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dec 19, 2013</th>\n",
       "      <td>33.31%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Nov 18, 2013</th>\n",
       "      <td>41.68%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2836 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   比例\n",
       "Date                 \n",
       "Mar 12, 2020  -37.19%\n",
       "Dec 18, 2013  -22.93%\n",
       "Jan 14, 2015  -20.45%\n",
       "Dec 06, 2013  -20.43%\n",
       "Dec 16, 2013  -19.81%\n",
       "...               ...\n",
       "Nov 21, 2013   21.56%\n",
       "Jul 20, 2017   24.13%\n",
       "Dec 07, 2017   25.47%\n",
       "Dec 19, 2013   33.31%\n",
       "Nov 18, 2013   41.68%\n",
       "\n",
       "[2836 rows x 1 columns]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#修改列名称\n",
    "df.columns= ['比例']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97f4a951",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
